//cube class
//kevin lyman

#include<windows.h>
#include<gl/Gl.h>
#include<gl/glut.h>
#include<cmath>
#include <math.h>
//#include "TCube.h"
//#include "TCube.cpp"


using namespace std;

class myCube
{
	float x,y,z;//for positioning
	float width,height,depth;//size of cube
	float xRot,yRot;//rotations
	
public:
	myCube() : x(0.0f), y(0.0f), z(0.0f),width(50.0f),height(50.0f),depth(50.0f),xRot(0.0f),yRot(0.0) {}
	myCube(float xp, float yp, float zp,float xwidth=100.0f,float yheight=100.0f, float zdepth=100.0f, float xrot=0.0f,float yrot=0.0f)
	{x = xp; y = yp; z=zp; width=xwidth; height=yheight; depth=zdepth; xRot=xrot, yRot=yrot;}
	
	void Render();
	void RotateX(float);
	void RotateY(float);
	void Position(float,float,float);
	void ChangeSize(float,float,float);
	//void Rotation();

};

void myCube::RotateX(float rotation)
{
	xRot = rotation;
}

void myCube::RotateY(float rotation)
{
	yRot = rotation;
}

void myCube::Position(float xMove, float yMove, float zMove)
{
	x=xMove;
	y=yMove;
	z=zMove;
}

void myCube::ChangeSize(float newWidth, float newHeight, float newDepth)
{
	width = newWidth;
	height = newHeight;
	depth = newDepth;
}

void myCube::Render()
{
	//float xposition = width;
	//float yposition = height;
	//float zposition = depth;
	
	glPushMatrix();

	glTranslatef(x, y, z);
    glRotatef(xRot, 1.0f, 0.0f, 0.0f);
    glRotatef(yRot, 0.0f, 1.0f, 0.0f);


    // Draw six quads to make a cube
    glBegin(GL_QUADS);
        // Front Face
        // White
        glColor3f(1.0f, 1.0f, 1.0f);
		glNormal3f(0.0f,0.0f,1.0f);
        glVertex3f(width,height,depth);
        glVertex3f(width,-height,depth);
        glVertex3f(-width,-height,depth);
        glVertex3f(-width,height,depth);

	
	// Back Face
        // Green
        glColor3f(0.0f, 1.0f, 0.0f);
		glNormal3f(0.0f,0.0f,-1.0f);
        glVertex3f(width,height,-depth);
        glVertex3f(width,-height,-depth);
        glVertex3f(-width,-height,-depth);
        glVertex3f(-width,height,-depth);
	
	// Top Face
        // Cyan
        glColor3f(0.0f, 1.0f, 1.0f);
		glNormal3f(0.0f,1.0f,0.0f);
        glVertex3f(width,height,-depth);
        glVertex3f(width,height,depth);
        glVertex3f(-width,height,depth);
        glVertex3f(-width,height,-depth);
	
	// Bottom Face
        // Yellow
        glColor3f(1.0f, 1.0f, 0.0f);
		glNormal3f(0.0f,-1.0f,0.0f);
        glVertex3f(width,-height,-depth);
        glVertex3f(width,-height,depth);
        glVertex3f(-width,-height,depth);
        glVertex3f(-width,-height,-depth);
	
	// Left face
        // Blue
        glColor3f(0.0f, 0.0f, 1.0f);
		glNormal3f(-1.0f,0.0f,0.0f);
        glVertex3f(width,height,depth);
        glVertex3f(width,height,-depth);
        glVertex3f(width,-height,-depth);
        glVertex3f(width,-height,depth);
	
	// Right face
        // Magenta
        glColor3f(1.0f, 0.0f, 1.0f);
		glNormal3f(1.0f,0.0f,0.0f);//right face normal sticks straight out to the right?
        glVertex3f(-width,height,depth);
        glVertex3f(-width,height,-depth);
        glVertex3f(-width,-height,-depth);
        glVertex3f(-width,-height,depth);
    glEnd();

	glPopMatrix();

}

class myTable
{


	float xRot;
	float yRot;
public:
	myTable();
	myTable(float,float);
	void RotateX(float);
	void RotateY(float);
	void Render();

};

myTable::myTable()
{

	
}

myTable::myTable(float s,float t)
{
	xRot = s;
	yRot = t;
}

void myTable::RotateX(float s)
{
	xRot = s;
}

void myTable::RotateY(float t)
{
	yRot = t;
}

void myTable::Render()
{
	glPushMatrix();
	// glTranslatef(x, y, z);
    glRotatef(xRot, 1.0f, 0.0f, 0.0f);
    glRotatef(yRot, 0.0f, 1.0f, 0.0f);



	glPopMatrix();
}

// rotation amounts
static GLfloat xRot = 0.0f;
static GLfloat yRot = 0.0f;
static GLfloat xPos = 0.0f;
static GLfloat yPos = 0.0f;
static GLfloat zPos = -900.0f;

//forward referencing
void RenderScene();
void SetupRC();
void KeyboardHandler(int key, int x, int y);
void ChangeSize(int w, int h);



void RenderScene(void)
{
    // clear the window 
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	
    

	//myCube testing;
	//testing.Position(xPos,yPos,zPos);
	//testing.RotateX(xRot);
	//testing.RotateY(yRot);
	//testing.Render();

	myTable table;
	table.RotateX(xRot);
	table.RotateY(yRot);
	table.Render();

    

    // display it
    glutSwapBuffers();
}


// context. 
void Init()
{
    // Black background
    glClearColor(0.0f, 0.0f, 0.0f, 1.0f );

    glEnable(GL_DEPTH_TEST);	
    glEnable(GL_DITHER);
    glShadeModel(GL_SMOOTH);
}

void keyBoardHandler(unsigned char key, int x, int y)
{
	switch(key)
	{
	case'z':
		zPos+=10.0f;
		break;
	case'Z':
		zPos-=10.0f;
		break;
	case'X':
		xPos+=10.0f;
		break;
	case'x':
		xPos-=10.0f;
		break;
	case'Y':
		yPos+=10.0f;
		break;
	case'y':
		yPos-=10.0f;
		break;
	default:/*nothing*/;
		break;
	}
	// Refresh the Window
	glutPostRedisplay();
}

// Get Arrow Keys
void SpecialKey(int key, int x, int y)
{
    if(key == GLUT_KEY_UP)
        xRot-= 5.0f;

    if(key == GLUT_KEY_DOWN)
        xRot += 5.0f;

    if(key == GLUT_KEY_LEFT)
        yRot -= 5.0f;

    if(key == GLUT_KEY_RIGHT)
        yRot += 5.0f;


    // Refresh the Window
    glutPostRedisplay();
}


void ChangeSize(int w, int h)
{
    GLfloat fAspect;

    // Prevent a divide by zero
    if(h == 0)
        h = 1;

    // Set Viewport to window dimensions
    glViewport(0, 0, w, h);

    // Reset coordinate system
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();

    fAspect = (GLfloat)w / (GLfloat)h;
    gluPerspective(55.0f, fAspect, 1.0f, 1000.0f);
     
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    //glTranslatef(0.0f, 0.0f, -900.0f);
}

/*
int main(int argc, char* argv[]) {
	glutInit(&argc, argv);
	glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
	glutInitWindowSize(800,600);
        glutCreateWindow("3D Cube");
	glutReshapeFunc(ChangeSize);
	glutSpecialFunc(SpecialKey);
	glutKeyboardFunc(keyBoardHandler);
	glutDisplayFunc(RenderScene);
	Init();
	glutMainLoop();

	return 0;
}
*/